package com.htjx.mall.controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.htjx.mall.commons.api.Result;
import com.htjx.mall.commons.models.CommonBean;
import com.htjx.mall.commons.models.User;
import com.htjx.mall.commons.models.enums.OrderStatus;
import com.htjx.mall.commons.models.user.Account;
import com.htjx.mall.controllers.commonapi.AuthorizeList;
import com.htjx.mall.services.OrderService;
import com.htjx.mall.services.UserService;
import com.htjx.mall.services.wallet.AccountService;
import com.htjx.mall.services.wallet.TicketService;

@Controller
@RequestMapping("/service")
public class ServiceController {

	@Autowired
	private UserService userService;
	@Autowired
	private OrderService orderService;
	@Autowired
	private TicketService ticketService;
	@Autowired
	private AccountService accountService;
	/****
	 * 用户中心
	 */
	@RequestMapping(produces = "application/json", method = RequestMethod.GET)
	@ResponseBody
	public Result service(HttpServletRequest request) {
		long userId = AuthorizeList.getCurrentAuthUser(request);
		User user = userService.findUniqueBy("id", String.valueOf(userId));
		if(user == null) {
			return new Result(9,"用户账号已过期，请重新登录");
		}
		// 查询订单信息
		List<CommonBean> commonbeanList = new ArrayList<CommonBean>();
		for(OrderStatus orderStatus: OrderStatus.values()) {
			if(orderStatus == OrderStatus.returning || orderStatus == OrderStatus.paid) {
				continue;
			}
			CommonBean commonBean = new CommonBean();
			commonBean.setIndex(orderStatus.getIndex());
			commonBean.setName(orderStatus.name());
			commonBean.setDescription(orderStatus.getDescription());
			int count = orderService.count(user, orderStatus);
			commonBean.setBody(count);
			commonbeanList.add(commonBean);
		}
		// 查询钱包信息
		//List<Ticket> ticketList = ticketService.selectTickets(user, new Date());
		//Wallet wallet = userService.selectWallet(user, new Date());
		Account account = accountService.selectOne(userId);
		
		Map<String, Object> retBody = new HashMap<String, Object>();
		retBody.put("user", user);
		retBody.put("commonbean_list", commonbeanList);
		retBody.put("account", account);
				
		Result result = new Result(0,"查询成功");
		result.setBody(retBody);
		return result;
	}
	
	/***
	 * 账户详情
	 */
	@RequestMapping(value="/account", produces = "application/json", method = RequestMethod.GET)
	@ResponseBody
	public Result account(HttpServletRequest request) {
		long userId = AuthorizeList.getCurrentAuthUser(request);
		User user = userService.findUniqueBy("id", String.valueOf(userId));
		if(user == null) {
			return new Result(9,"用户账号已过期，请重新登录");
		}
		//查询现金抵用券Ticket
		//List<Ticket> ticketList = ticketService.selectTickets(user, new Date());
		//Wallet wallet = userService.selectWallet(user, new Date());
		Account account = accountService.selectOne(userId);
		
		Map<String, Object> retBody = new HashMap<String, Object>();
		retBody.put("account", account);
		Result result = new Result(0,"查询成功");
		result.setBody(retBody);
		return result;
	}
	
}
